package com.dingshang.dsapp.service;

import com.dingshang.dsapp.entity.Picture;
import com.dingshang.dsapp.mapper.PictureMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PictureService {

    @Value("${file.image.url}")
    protected String fileUrl;

    @Autowired
    private PictureMapper pictureMapper;

    public void addHomePicture(String picIntroduce, String picTitle, String picName) {
        final Picture picture = new Picture();
        picture.setPicUrl(picName);
        picture.setPicDesc(picIntroduce);
        picture.setPicTitle(picTitle);
        picture.setPicType(1);
        pictureMapper.insert(picture);
    }

    public List<Picture> listHomePicture() {
        List<Picture> pictures = pictureMapper.listPicture(1);
        for (Picture pic : pictures) {
            pic.setPicUrl(fileUrl + pic.getPicUrl());
            pic.setPicDesc(pic.getPicDesc().length() > 10 ? pic.getPicDesc().substring(0,10) + "..." : pic.getPicDesc());
        }
        return pictures;
    }

    public void delHomePicture(int id) {
        pictureMapper.deleteByPrimaryKey(id);
    }
}
